package com.xyauto.theme.utils

import com.blankj.utilcode.util.LogUtils
import com.xyauto.theme.bean.ISubResBean
import com.xyauto.theme.bean.IThemeBean
import com.xyauto.theme.bean.LocalThemeBean
import com.xyauto.theme.bean.SubIconResBean
import java.io.File


object ThemeFileManager {


    private val ThemeFilter: (dir: File, name: String) -> Boolean = { _, name ->
        name.endsWith(".apk")
    }

    private val ImageFilter: (dir: File, name: String) -> Boolean = { _, name ->
        val fileEnd = name.substringAfter(".").lowercase()
        fileEnd == "jpg" || fileEnd == "png" || fileEnd == "gif" || fileEnd == "jpeg" || fileEnd == "bmp" || fileEnd == "webp"
    }

    /**
     * 搜索主题ByPath
     */
    fun scanThemeByPath(path: String): MutableList<IThemeBean> {
        LogUtils.i("scanThemeByPath: $path")
        val list = arrayListOf<IThemeBean>()
        val file = File(path)
        if (!file.exists() or !file.isDirectory) {
            LogUtils.i("scanThemeByPath: ${path};not exists or not isDirectory")
            return list
        }
        val strings = file.list() ?: return list

        for (name in strings) {
            val dir = File(file, name)
            if (dir.isDirectory) {
                val fileList = dir.list() ?: continue
                var apkName = ""
                val arrayList = arrayListOf<String>()
                //单launcher主题
                for (itemName in fileList) {
                    if (apkName.isEmpty() and ThemeFilter.invoke(dir, itemName)) {
                        apkName = "${dir.path}/${itemName}"
                    }
                    if (ImageFilter.invoke(dir, itemName)) {
                        arrayList.add("${dir.path}/${itemName}")
                    }
                }
                if (apkName.isNotEmpty() and arrayList.isNotEmpty()) {
                    list.add(LocalThemeBean(name, arrayList.first(), apkName, arrayList))
                }
            }
        }
        return list
    }

    /**
     * 搜索图标资源ByPath
     */
    fun scanIconResByPath(path: String): MutableList<ISubResBean> {
        LogUtils.i("scanIconResByPath: $path")
        val list = arrayListOf<ISubResBean>()
        val file = File(path)
        if (!file.exists() or !file.isDirectory) {
            LogUtils.i("scanIconResByPath: ${path};not exists or not isDirectory")
            return list
        }
        val strings = file.list() ?: return list

        for (name in strings) {
            val dir = File(file, name)
            if (dir.isDirectory) {
                val fileList = dir.list() ?: continue
                var apkName = ""
                var imageName = ""
                //单launcher主题
                for (itemName in fileList) {
                    when {
                        apkName.isEmpty() and ThemeFilter.invoke(dir, itemName) -> apkName = itemName
                        imageName.isEmpty() and ImageFilter.invoke(dir, itemName) -> imageName = itemName
                    }
                    if (apkName.isNotEmpty() and imageName.isNotEmpty()) {
                        list.add(SubIconResBean(name, "${dir.path}/${imageName}", "${dir.path}/${apkName}"))
                        break
                    }
                }
            }
        }
        val def = File(file, "default.png")
        if (def.exists()) {
            list.add(0, SubIconResBean("default", def.absolutePath, ""))
        }
        return list
    }

}


